//
//  FileManager.m
//  Traffic
//
//  Created by CruItaly on 2017/10/28.
//  Copyright © 2017年 com.infohold.cn. All rights reserved.
//

#import "FileManager.h" 
@implementation FileManager

+ (void)writeFile:(id)data filePath:(NSString*)filePath fileName:(NSString *)file{
    NSFileManager *fileManage = [NSFileManager defaultManager];
    @synchronized (self) {
        if(![fileManage fileExistsAtPath:filePath]){
            [fileManage createDirectoryAtPath:filePath withIntermediateDirectories:YES attributes:nil error:nil];
        }
        //将数据转化为二进制文件数据
        NSData *tem_file=[NSKeyedArchiver archivedDataWithRootObject:data];
        NSString *tem_filePath = [filePath stringByAppendingPathComponent:file];
        [fileManage createFileAtPath:tem_filePath contents:tem_file attributes:nil];
    }
}

+ (id)readFilePath:(NSString *)path fileName:(NSString *)file className:(id)obj{
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSString *filePath = [path stringByAppendingPathComponent:file];
    @synchronized (self) {
        if([fileManage fileExistsAtPath:filePath]){
            return [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
        }
        return nil;
    }
}
+ (void)removeFilePath:(NSString *)path fileName:(NSString *)fileName{
    NSFileManager *fileManage = [NSFileManager defaultManager];
    NSString *filePath = [path stringByAppendingPathComponent:fileName];
    @synchronized (self) {
        if([fileManage fileExistsAtPath:filePath]){
            [fileManage removeItemAtPath:filePath error:nil];
        }
    }
}
@end
